package com.woke.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.GlideApp;
import com.woke.data.UnionPayChannel;
import com.zhongcai.online.R;

import java.util.HashSet;
import java.util.List;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/12
 * 备注：   商家专用收款码通道,开通多个通道
 */

public class UnionPayChannelMultiSelectAdapter extends RecyclerView.Adapter<UnionPayChannelMultiSelectAdapter.ViewHolder> {

	private LayoutInflater mInflater;
	private Context mContext;
	private List<UnionPayChannel> mChannels;
	private HashSet<Integer> mMultiChannelsWay = new HashSet<> ();


	public UnionPayChannelMultiSelectAdapter(Context context, List<UnionPayChannel> list) {
		mInflater = LayoutInflater.from (context);
		this.mContext = context;
		this.mChannels = list;
		mMultiChannelsWay.clear ();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new ViewHolder (mInflater.inflate (R.layout.adapter_unionpay_channel, parent, false));
	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, final int position) {
		final UnionPayChannel channel = mChannels.get (position);

		if (channel != null) {
			String imgPath = channel.getIcon ();
			holder.mTitle.setText (channel.getTitle ());
			if (! TextUtils.isEmpty (imgPath)) {
				if (! imgPath.startsWith ("http")) {
					imgPath = "http://" + imgPath;
				}
				GlideApp.with (mContext)
						.load (imgPath)
						.placeholder(R.mipmap.icn__launch_oem)
						.error(R.mipmap.icn__launch_oem)
						.into (holder.mIcon);
			}
			holder.mChannelWayCb.setOnCheckedChangeListener (null);
			if (channel.isEnable ()) {
				mMultiChannelsWay.add (channel.getWay ());
				holder.mChannelWayCb.setChecked (true);
			} else {
				if (mMultiChannelsWay.contains (channel.getWay ())) {
					mMultiChannelsWay.remove (channel.getWay ());
				}
				holder.mChannelWayCb.setChecked (false);
			}
			holder.mChannelWayCb.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						mMultiChannelsWay.add (channel.getWay ());
						channel.setEnableActivate (true);
					} else {
						if (mMultiChannelsWay.contains (channel.getWay ())) {
							mMultiChannelsWay.remove (channel.getWay ());
						}
						channel.setEnableActivate (false);
					}
					notifyItemChanged (position, channel);

				}
			});
			holder.itemView.setOnClickListener (new View.OnClickListener () {
				@Override
				public void onClick(View v) {

					if (holder.mChannelWayCb.isChecked ()) {
						holder.mChannelWayCb.setChecked (false);
					} else {
						holder.mChannelWayCb.setChecked (true);
					}
				}
			});

		}

	}


	@Override
	public int getItemCount() {
		return mChannels == null ? 0 : mChannels.size ();
	}


	protected class ViewHolder extends RecyclerView.ViewHolder {

		private ImageView mIcon;
		private TextView mTitle;
		private AppCompatCheckBox mChannelWayCb;

		public ViewHolder(View itemView) {
			super (itemView);
			mIcon = (ImageView) itemView.findViewById (R.id.channel_img);
			mTitle = (TextView) itemView.findViewById (R.id.channel_title);
			mChannelWayCb = (AppCompatCheckBox) itemView.findViewById (R.id.channel_cb);

		}
	}

	public int[] getWay() {
		int size = mMultiChannelsWay.size ();
		if (size > 0) {
			Integer[] integer = mMultiChannelsWay.toArray (new Integer[size]);
			int[] ways = new int[size];
			for (int i = 0; i < integer.length; i++) {
				ways[i] = integer[i].intValue ();
			}
			return ways;
		}
		return new int[]{};
	}
}
